package com.zenika.zentracker.jmx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.zenika.zentracker.hibernate.info.UpdatedObjectInfo;
import com.zenika.zentracker.hibernate.interceptor.SessionStatisticsInterceptor;

public class JMXSessionStatsImpl implements JMXSessionStats {

    private List loadedObjects = new ArrayList();
    private List updatedObjects = new ArrayList();
    private List createdObjects = new ArrayList();
    private List deletedObjects = new ArrayList();
    private List dirtyObjects = new ArrayList();
    private List collectionUpdates = new ArrayList();
    private List executionTimes = new ArrayList();

    public JMXSessionStatsImpl(SessionStatisticsInterceptor sessionStatisticsInterceptor) {
        this.loadedObjects.addAll(createListFromMap(sessionStatisticsInterceptor.getLoadedObjects()));
        this.updatedObjects.addAll(createListFromMap(sessionStatisticsInterceptor.getUpdatedObjects()));
        this.createdObjects.addAll(createListFromMap(sessionStatisticsInterceptor.getCreatedObjects()));
        this.deletedObjects.addAll(createListFromMap(sessionStatisticsInterceptor.getDeletedObjects()));
        this.dirtyObjects.addAll(createDirtyObjectsList(sessionStatisticsInterceptor.getDirtyObjects()));
        this.collectionUpdates.addAll(createListFromMap(sessionStatisticsInterceptor.getCollectionActivity()));
        this.executionTimes.addAll(sessionStatisticsInterceptor.getExecutionTimes());
    }

    private List createDirtyObjectsList(List dirtyObjects2) {
        List newList = new ArrayList();

        for (Iterator iterator = dirtyObjects2.iterator(); iterator.hasNext();) {
            UpdatedObjectInfo objectInfo = (UpdatedObjectInfo) iterator.next();
            newList.add(objectInfo.toString());
        }

        return newList;
    }

    private List createListFromMap(Map objectsMap) {
        List newList = new ArrayList();
        Iterator iterator = objectsMap.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry keyValue = (Map.Entry) iterator.next();

            newList.add(keyValue.getKey() + " - " + keyValue.getValue());
        }

        return newList;
    }

    public List getLoadedObjects() {
        return loadedObjects;
    }

    public List getUpdatedObjects() {
        return updatedObjects;
    }

    public List getCreatedObjects() {
        return createdObjects;
    }

    public List getDeletedObjects() {
        return deletedObjects;
    }

    public List getDirtyObjects() {
        return dirtyObjects;
    }

    public List getCollectionUpdates() {
        return collectionUpdates;
    }

    public List getExecutionTimes() {
        return executionTimes;
    }

}